﻿package mortal.game.view.common.guide.navbar
{
    import com.greensock.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class NavbarButtonUIRow extends Object
    {
        private var _container:DisplayObjectContainer;
        private var _btns:Array;
        private var _redPoints:Dictionary;
        private var _showStatusList:Array;
        private var _btnNames:Array;
        private var _startPoint:Point;
        private var _shopPoint:Point;
        private const DistanceX:int = 40;
        private const ShowDelayTime:int = 400;
        private var _tweenShowCallback:Function;
        private static var _instance:NavbarButtonUIRow;

        public function NavbarButtonUIRow()
        {
            this._redPoints = new Dictionary();
            this._startPoint = new Point();
            return;
        }// end function

        public function showHideFilter(param1:String, param2:Boolean = true, param3:int = 0) : void
        {
            var _loc_4:* = this.getButton(param1);
            if (_loc_4 == null)
            {
                return;
            }
            if (param2)
            {
                EffectManager.glowFilterReg(_loc_4, null, 1, 10, 0, param3);
            }
            else
            {
                EffectManager.glowFilterUnReg(_loc_4);
            }
            return;
        }// end function

        public function showHideRedPoint(param1:String, param2:Boolean = true) : void
        {
            var _loc_3:GBitmap = null;
            var _loc_4:DisplayObject = null;
            if (!param2)
            {
                _loc_3 = this._redPoints[param1] as GBitmap;
                if (_loc_3 != null)
                {
                    _loc_3.dispose(true);
                    delete this._redPoints[param1];
                }
            }
            else if (this._redPoints[param1] == null)
            {
                _loc_4 = this.getButton(param1);
                if (_loc_4 == null || _loc_4.parent == null || _loc_4.visible == false)
                {
                    return;
                }
                this._redPoints[param1] = UIFactory.gBitmap(ImagesConst.SmallRedPoint, _loc_4.x + _loc_4.width - 15, _loc_4.y + 4, _loc_4.parent);
            }
            return;
        }// end function

        public function isShowingRePoint(param1:String) : Boolean
        {
            return this._redPoints[param1] != null;
        }// end function

        public function setLightEffect(param1:String, param2:Boolean = true) : void
        {
            var _loc_3:* = this.getButton(param1);
            if (_loc_3 == null)
            {
                return;
            }
            if (param2)
            {
                EffectManager.glowFilterReg(_loc_3);
            }
            else
            {
                EffectManager.glowFilterUnReg(_loc_3);
            }
            return;
        }// end function

        private function getButton(param1:String) : DisplayObject
        {
            if (this._btnNames == null)
            {
                return null;
            }
            var _loc_2:* = this._btnNames.indexOf(param1);
            if (_loc_2 < 0)
            {
                return null;
            }
            return this._btns[_loc_2] as DisplayObject;
        }// end function

        public function get showStatusList() : Array
        {
            if (this._showStatusList == null)
            {
                return [];
            }
            return this._showStatusList.concat([]);
        }// end function

        public function init(param1:DisplayObjectContainer, param2:Array) : void
        {
            var _loc_4:DisplayObject = null;
            var _loc_5:int = 0;
            this._container = param1;
            this._btnNames = param2;
            this._btns = [];
            var _loc_3:int = 0;
            while (_loc_3 < param1.numChildren)
            {
                
                _loc_4 = param1.getChildAt(_loc_3);
                _loc_5 = this._btnNames.indexOf(_loc_4.name);
                if (_loc_5 >= 0)
                {
                    this._btns[_loc_5] = _loc_4;
                    if (_loc_5 == 1)
                    {
                        this._startPoint.y = _loc_4.y;
                    }
                    if (_loc_5 == 0)
                    {
                        this._shopPoint = new Point(_loc_4.x, _loc_4.y);
                        this._startPoint.x = _loc_4.x;
                    }
                }
                _loc_3++;
            }
            this.updateButtonsLayout();
            return;
        }// end function

        public function initShow(param1:Array) : void
        {
            if (param1 == null || param1.length == 0)
            {
                return;
            }
            this._showStatusList = param1;
            this.updateButtonsLayout();
            return;
        }// end function

        private function updateButtonsLayout() : void
        {
            var _loc_3:Boolean = false;
            var _loc_4:DisplayObject = null;
            var _loc_5:GBitmap = null;
            if (this._showStatusList == null || this._btns == null)
            {
                return;
            }
            var _loc_1:* = this._startPoint.x;
            var _loc_2:int = 0;
            while (_loc_2 < this._showStatusList.length)
            {
                
                _loc_3 = this._showStatusList[_loc_2] as Boolean;
                _loc_4 = this._btns[_loc_2] as DisplayObject;
                if (_loc_4 == null)
                {
                }
                else if (_loc_3)
                {
                    _loc_4.visible = true;
                    _loc_4.x = _loc_1;
                    _loc_4.y = this._startPoint.y;
                    _loc_1 = _loc_1 - this.DistanceX;
                    if (_loc_2 == 0 && this._shopPoint != null)
                    {
                        _loc_4.x = this._shopPoint.x;
                        _loc_4.y = this._shopPoint.y;
                    }
                    _loc_5 = this._redPoints[_loc_4.name];
                    if (_loc_5 != null)
                    {
                        _loc_5.x = _loc_4.x + _loc_4.width - 15;
                    }
                }
                else
                {
                    _loc_4.visible = false;
                    if (_loc_5 != null)
                    {
                        DisplayUtil.removeMe(_loc_5);
                    }
                }
                _loc_2++;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.NavBarButtonPosUpdate));
            return;
        }// end function

        public function getCurPlace(param1:String) : Point
        {
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Boolean = false;
            var _loc_2:* = this._btnNames.indexOf(param1);
            var _loc_3:* = new Point(0, 0);
            if (_loc_2 == -1)
            {
                return _loc_3;
            }
            var _loc_4:* = this._btns[_loc_2] as DisplayObject;
            if (_loc_4.visible == true)
            {
                _loc_3 = _loc_4.localToGlobal(_loc_3);
            }
            else
            {
                _loc_5 = this.getIndexFromShowingList(_loc_2);
                _loc_6 = _loc_5 + 1;
                while (_loc_6 < this._showStatusList.length)
                {
                    
                    _loc_4 = this._btns[_loc_6] as DisplayObject;
                    _loc_7 = this._showStatusList[_loc_6] as Boolean;
                    if (_loc_7)
                    {
                        _loc_3 = _loc_4.localToGlobal(_loc_3);
                        return _loc_3;
                    }
                    _loc_6++;
                }
            }
            return _loc_3;
        }// end function

        public function tweenShowIcon(param1:String, param2:Function = null) : void
        {
            var _loc_7:DisplayObject = null;
            var _loc_8:Boolean = false;
            var _loc_9:GBitmap = null;
            this._tweenShowCallback = param2;
            var _loc_3:* = this._btnNames.indexOf(param1);
            if (_loc_3 == -1 || this._showStatusList[_loc_3])
            {
                if (this._tweenShowCallback != null)
                {
                    this._tweenShowCallback.apply();
                }
                return;
            }
            var _loc_4:* = this.getIndexFromShowingList(_loc_3);
            this._showStatusList[_loc_3] = true;
            var _loc_5:int = 600;
            setTimeout(this.tweenShowIconComplete, _loc_5);
            var _loc_6:* = _loc_4 + 1;
            while (_loc_6 < this._showStatusList.length)
            {
                
                _loc_7 = this._btns[_loc_6] as DisplayObject;
                _loc_8 = this._showStatusList[_loc_6] as Boolean;
                if (_loc_8)
                {
                    TweenMax.to(_loc_7, _loc_5 / 1000 - 0.1, {x:_loc_7.x - this.DistanceX});
                    _loc_9 = this._redPoints[_loc_7.name];
                    if (_loc_9 != null)
                    {
                        TweenMax.to(_loc_9, _loc_5 / 1000 - 0.1, {x:_loc_7.x + _loc_7.width - 15 - this.DistanceX});
                    }
                }
                _loc_6++;
            }
            return;
        }// end function

        private function tweenShowIconComplete() : void
        {
            this.updateButtonsLayout();
            if (this._tweenShowCallback != null)
            {
                this._tweenShowCallback.apply();
            }
            return;
        }// end function

        private function getIndexFromShowingList(param1:int) : int
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                if (this._showStatusList[_loc_3])
                {
                    _loc_2++;
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        public static function get instance() : NavbarButtonUIRow
        {
            if (_instance == null)
            {
                _instance = new NavbarButtonUIRow;
            }
            return _instance;
        }// end function

    }
}
